﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.DAL;
using CMS.Entity;
using System.Data;

namespace CMS.BLL
{
    public class TrainBLL
    {
        /// <summary>
        /// 检索培训列表信息
        /// </summary>
        /// <param name="pageindex">页码</param>
        /// <param name="begin">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="title">会议标题</param>
        /// <returns></returns>
        public string GetTrainList(int pageindex, DateTime begin, DateTime end, string title, string type)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<tr>");
            sb.Append("<th width=\"12%\">主题</th>");
            sb.Append("<th width=\"6%\">类别</th>");
            sb.Append("<th  width=\"12%\">开始时间</th>");
            sb.Append("<th width=\"12%\" >结束时间</th>");
            sb.Append("<th width=\"10%\"  >讲师</th>");
            sb.Append("<th width=\"6%\" >限制人数</th>");
            sb.Append("<th width=\"20%\"  >网址</th>");
            sb.Append("<th >密码</th>");
            sb.Append("<th width=\"10%\"  >操作</th>");
            sb.Append("</tr>");

            try
            {
                TrainDAL ac = new TrainDAL();
                List<TrainEnty> list = ac.GetTrainList(pageindex, begin, end, title, type);

                int len = 15;
                if (list.Count < len)
                    len = list.Count;
                for (int i = 0; i < len; i++)
                {
                    string ttype = list[i].Type.ToString();
                    if (ttype == "ZJMDM")
                        ttype = "专家面对面";
                    else if (ttype == "ZXHC")
                        ttype = "在线会场";

                    if (i % 2 == 0)
                    {
                        sb.Append("<tr align=\"left\">");
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"12%\">{0}</td>", CutStr(list[i].Title, 12));
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"6%\">{0}</td>", ttype);
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"12%\">{0}</td>", string.Format("{0:yyyy-MM-dd HH:mm}", list[i].BeginTime));
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"12%\">{0}</td>", string.Format("{0:yyyy-MM-dd HH:mm}", list[i].EndTime));
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"10%\">{0}</td>", list[i].Lecturer);
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"6%\">{0}</td>", list[i].PerCount);
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"20%\">{0}</td>", CutStr(list[i].Address, 20));
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" >{0}</td>", CutStr(list[i].Content, 25));
                        sb.AppendFormat("<td  class=\"tablerow2 hovered\" width=\"10%\"><a href=\"TrainDetail.aspx?trid={0}\"><img src=\"../../images/bt_edit.gif\" alt=\"修改\" title=\"修改\" /></a>|<a href=\"javascript:void(0);\" onclick='del(\"{0}\")'><img src=\"../../images/bt_del.gif\" alt=\"删除\" /></a></td>", list[i].Tid);
                        sb.Append("</tr>");
                    }
                    else
                    {
                        sb.Append("<tr align=\"left\">");
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"12%\">{0}</td>", CutStr(list[i].Title, 12));
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"6%\">{0}</td>", ttype);
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"12%\">{0}</td>", string.Format("{0:yyyy-MM-dd HH:mm}", list[i].BeginTime));
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"12%\">{0}</td>", string.Format("{0:yyyy-MM-dd HH:mm}", list[i].EndTime));
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"10%\">{0}</td>", list[i].Lecturer);
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"6%\">{0}</td>", list[i].PerCount);
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"20%\">{0}</td>", CutStr(list[i].Address, 20));
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" >{0}</td>", CutStr(list[i].Content, 25));
                        sb.AppendFormat("<td  class=\"tablerow1 hovered\" width=\"10%\"><a href=\"TrainDetail.aspx?trid={0}\"><img src=\"../../images/bt_edit.gif\" alt=\"修改\" title=\"修改\" /></a>|<a href=\"javascript:void(0);\" onclick='del(\"{0}\")'><img src=\"../../images/bt_del.gif\" alt=\"删除\" /></a></td>", list[i].Tid);
                        sb.Append("</tr>");
                    }
                }
            }
            catch
            {
                sb = new StringBuilder();
            }

            return sb.ToString();
        }

        /// <summary>
        /// 构造分页信息
        /// </summary>
        /// <param name="pageindex">当前页</param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public string CountPage(int pageindex, DateTime begin, DateTime end, string title, string type)
        {
            StringBuilder result = new StringBuilder();
            TrainDAL ac = new TrainDAL();
            object obj = ac.CountPage(begin, end, title, type);
            int count = Convert.ToInt32(obj.ToString());
            int page = (count / 10) == 0 ? 1 : (count % 10 == 0 ? count / 10 : (count / 10) + 1);
            //总数
            result.Append("<div style=\"float:left;\">");
            result.AppendFormat("记录总数：<font color='red'>{0}条</font>  当前页：<font color='red'>{1}</font>/<font color='red'>{2}</font> 页  每页显示记录数：<font color='red'> 10条</font>  ", count, pageindex, page);
            result.Append("</div>");
            //分页
            if (page == 1)
            {
                result.Append("<a href=\"javascript:void(0);\" >&nbsp;&nbsp; 首页</a> | <a href=\"javascript:void(0);\" >下页</a> | <a href=\"javascript:void(0);\" >上页</a> | <a href=\"javascript:void(0);\" >尾页</a>");
            }
            else
            {
                int next = pageindex == page ? page : pageindex + 1;
                int per = pageindex == 1 ? 1 : pageindex - 1;
                result.AppendFormat("<a href=\"javascript:void(0);\" onclick=\" Get(1, '{1}' , '{2}','{3}');\" >&nbsp;&nbsp; 首页</a> | <a href=\"javascript:void(0);\"  onclick=\" Get({0}, '{1}' , '{2}','{3}');\"  >下页</a> | <a href=\"javascript:void(0);\"  onclick=\" Get({4}, '{1}' , '{2}','{3}');\" >上页</a> | <a href=\"javascript:void(0);\" onclick=\" Get({5}, '{1}' , '{2}','{3}');\"  >尾页</a>", next, begin, end, title, per, page);
            }

            return result.ToString();
        }

        /// <summary>
        /// 根据id，获取会场详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GteTrainById(string id)
        {
            StringBuilder sb = new StringBuilder();
            TrainDAL ac = new TrainDAL();
            List<TrainEnty> train = ac.GetTrainById(id);
            if (train != null)
            {
                foreach (TrainEnty t in train)
                {
                    sb.AppendFormat("{0}&{1}&{2}&{3}&{4}&{5}&{6}&{7}&{8}", t.Tid, t.Title, t.BeginTime, t.EndTime, t.Lecturer, t.PerCount, t.Address, t.Content, t.Type);
                }
            }
            return sb.ToString();
        }

        public int ModifyById(string id, string title, DateTime begin, DateTime end, string lec, int percount, string add, string content, string type)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("tid", System.Data.SqlDbType.BigInt, id);
            tool.AddSqlParameter("title", System.Data.SqlDbType.VarChar, title);
            tool.AddSqlParameter("begin", System.Data.SqlDbType.DateTime, begin);
            tool.AddSqlParameter("end", System.Data.SqlDbType.DateTime, end);

            tool.AddSqlParameter("lecturer", System.Data.SqlDbType.VarChar, lec);
            tool.AddSqlParameter("percount", System.Data.SqlDbType.Int, percount);
            tool.AddSqlParameter("address", System.Data.SqlDbType.VarChar, add);
            tool.AddSqlParameter("content", System.Data.SqlDbType.VarChar, content);
            tool.AddSqlParameter("type", System.Data.SqlDbType.VarChar, type);
            TrainDAL ac = new TrainDAL();
            int res = ac.ModifyTrainById(tool.GetSqlParameters());
            return res;
        }

        public int DelById(string id)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("tid", System.Data.SqlDbType.BigInt, id);
            TrainDAL ac = new TrainDAL();
            int res = ac.DelById(tool.GetSqlParameters());
            return res;
        }

        /// <summary>
        /// 在线会场培训 前6条
        /// </summary>
        /// <returns></returns>
        public string GetTop6Train()
        {
            StringBuilder result = new StringBuilder();
            TrainDAL dal = new TrainDAL();
            List<TrainEnty> list = dal.GetTop6Train();

            for (int i = 0; i < list.Count; i++)
            {
                if (i % 2 == 0)
                {
                    result.AppendLine("<tr>");
                }
                else
                {
                    result.AppendLine("<tr class=\"lineodd\">");
                }
                result.AppendFormat("<td>{0}</td>", list[i].Title);
                result.AppendFormat("<td>{0}</td>", string.Format("{0:yyyy-MM-dd/HH:mm}", list[i].BeginTime));
                result.AppendFormat("<td>{0}</td>", string.Format("{0:yyyy-MM-dd/HH:mm}", list[i].EndTime));
                result.AppendFormat("<td>{0}</td>", list[i].Lecturer);
                result.AppendFormat("<td>{0}</td>", list[i].PerCount);

                //不需要跟预订表中的预订数进行对比？？需求文档说对比时间
                if (list[i].BeginTime < DateTime.Now)
                    result.AppendFormat("<td><a class=\"bookyes\" onclick='order(\"{0}\")'></a></td>", list[i].Tid);
                else
                    result.Append("<td><a class=\"bookno\"></a></td>");

                result.AppendLine("</tr>");
            }

            return result.ToString();
        }

        /// <summary>
        /// 专家面对面最新一条
        /// </summary>
        /// <returns></returns>
        public string GetTop1Train()
        {
            StringBuilder result = new StringBuilder();
            TrainDAL dal = new TrainDAL();
            List<TrainEnty> list = dal.GetTop1Train();
            if (list.Count == 1)
            {
                result.AppendFormat("<h2 class=\"red\">{0}</h2>", list[0].Title);
                result.Append("<div class=\"lcnt_l\">");
                result.AppendFormat("<div>{0}</div>", list[0].Content);
                //此处要计算剩余人数
                result.AppendFormat("<div class=\"metbookseat\">{0}</div>", CountperByTrainId(list[0].Tid.ToString()));
                result.Append("</div>");
                result.Append("<div class=\"lcnt_r\">");
                result.Append("<div class=\"meteacher\">");
                result.AppendFormat("<div class=\"metline\">特约讲师：{0}</div>", list[0].Lecturer);
                //计算星期数
                System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("zh-CN");
                result.AppendFormat("<div class=\"metline\">开讲时间：{0} ({1})</div>",
                    string.Format("{0:M月dd日}", list[0].BeginTime), cultureInfo.DateTimeFormat.GetAbbreviatedDayName(list[0].BeginTime.DayOfWeek).ToString());

                result.Append("</div>");
                result.AppendFormat("<input id=\"hidTid\" type=\"hidden\" value=\"{0}\" />", list[0].Tid);

            }
            return result.ToString();
        }

        /// <summary>
        /// 按会场id获取该会场剩余坐席
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string CountperByTrainId(string tid)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("tid", System.Data.SqlDbType.BigInt, tid);
            TrainDAL ac = new TrainDAL();
            object r = ac.CountPerByTrainId(tool.GetSqlParameters());

            return r.ToString();
        }

        /// <summary>
        /// 新增培训信息
        /// </summary>
        /// <param name="title">会议主题</param>
        /// <param name="begin">开始时间</param>
        /// <param name="end">结束时间</param>
        /// <param name="lec">讲师</param>
        /// <param name="percount">限制人数</param>
        /// <param name="add">地址</param>
        /// <param name="content">内容</param>
        /// <returns></returns>
        public int AddTrain(string title, DateTime begin, DateTime end, string lec, int percount, string add, string content, string type)
        {
            Tools tool = new Tools();
            tool.AddSqlParameter("title", System.Data.SqlDbType.VarChar, title);
            tool.AddSqlParameter("begin", System.Data.SqlDbType.DateTime, begin);
            tool.AddSqlParameter("end", System.Data.SqlDbType.DateTime, end);

            tool.AddSqlParameter("lecturer", System.Data.SqlDbType.VarChar, lec);
            tool.AddSqlParameter("percount", System.Data.SqlDbType.Int, percount);
            tool.AddSqlParameter("address", System.Data.SqlDbType.VarChar, add);
            tool.AddSqlParameter("content", System.Data.SqlDbType.VarChar, content);
            tool.AddSqlParameter("type", System.Data.SqlDbType.VarChar, type);

            TrainDAL ac = new TrainDAL();
            int res = ac.AddTrain(tool.GetSqlParameters());
            return res;
        }

        /// <summary>
        /// 根据会场id取会场地址
        /// </summary>
        /// <param name="tid"></param>
        /// <returns></returns>
        public string GetAddressByTrainId(string tid)
        {
            TrainDAL ac = new TrainDAL();
            object obj = ac.GetAddressByTrainId(tid);
            return obj.ToString();
        }

        /// <summary>
        /// 获取该地区最新一条有效会场记录
        /// </summary>
        /// <param name="area"></param>
        /// <returns></returns>
        public string GetZJMDM(string area)
        {
            StringBuilder result = new StringBuilder();
            TrainDAL ac = new TrainDAL();
            DataSet ds = ac.GetZJTrain(area);
            if (ds != null)
            {
                //同时需要第一条会场的详细信息
                if (ds.Tables[0].Rows.Count > 0)
                {
                    result.AppendFormat("<h2 class=\"red\">{0}</h2>", ds.Tables[0].Rows[0]["place_name"]);
                    result.Append("<div class=\"lcnt_l\">");
                    result.AppendFormat("<div>{0}</div>", ds.Tables[0].Rows[0]["train_intro"]);
                    //此处要计算剩余人数
                    result.AppendFormat("<div class=\"metbookseat\">{0}</div>", int.Parse(ds.Tables[0].Rows[0]["allnum"].ToString()) - int.Parse(ds.Tables[0].Rows[0]["num"].ToString()));
                    result.Append("</div>");
                    result.Append("<div class=\"lcnt_r\">");
                    result.Append("<div class=\"meteacher\">");
                    result.AppendFormat("<div class=\"metline\">特约讲师：{0}</div>", ds.Tables[0].Rows[0]["teachername"]);

                    //计算星期数，正式数据库中的日期是**月**日(周*) **：** 这种形式 
                    System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("zh-CN");
                    string mm = ds.Tables[0].Rows[0]["train_time"].ToString();
                    int len = mm.IndexOf("周");
                    if (len >= 0)
                        mm = mm.Substring(0, (mm.IndexOf("周") + 3));

                    result.AppendFormat("<div class=\"metline\" id=\"traintime\">开讲时间：{0} </div>", mm);

                    result.Append("</div>");
                    result.AppendFormat("<input id=\"hidid\" type=\"hidden\" value=\"{0}\" />", ds.Tables[0].Rows[0]["id"]);
                    result.AppendFormat("<input id=\"hidarea\" type=\"hidden\" value=\"{0}\" />", ds.Tables[0].Rows[0]["area"]);

                    result.Append("<div class=\"metline\">手机号：");
                    result.Append("<input name=\"txtTel\" id=\"txtTel\" type=\"text\" value=\"\" class=\"tinp booktel\"  maxlength=\"11\"/>");
                    result.Append("&nbsp;<input type=\"button\" value=\"\" class=\"bookcode\" onclick=\"GetCheckCode()\"/>");
                    result.Append("</div>");
                    result.Append("<div class=\"metline\">验证码：");
                    result.Append("<input name=\"txtCode\" id=\"txtCode\" maxlength=\"6\" type=\"text\" class=\"tinp booknumber\" />");
                    result.Append("</div>");
                    result.Append("<div class=\"metsubmit\">");
                    result.Append("<div class=\"metremind\">会场信息会以短信形式发送至您手机上。</div>");
                    result.Append("<input id=\"submit1\" type=\"image\" src=\"/images/met_book.jpg\" />");
                    result.Append("</div>");
                    result.Append("</div>");
                    result.Append("<div class=\"clr\"></div>");
                }
                else
                    result.Append(GetZJMDMById("0"));
            }
            else
            {
                result.Append(GetZJMDMById("0"));
            }

            return result.ToString();

        }

        /// <summary>
        /// 根据id获取对应的会场信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetZJMDMById(string id)
        {
            StringBuilder result = new StringBuilder();
            if (id != "0")
            {
            }
            else
            {
                result.AppendFormat("<h2 class=\"red\">{0}</h2>", "无");
                result.Append("<div class=\"lcnt_l\">");
                result.AppendFormat("<div>{0}</div>", "  暂无相关培训  ");
                //此处要计算剩余人数
                result.AppendFormat("<div class=\"metbookseat\">{0}</div>", "0");
                result.Append("</div>");
                result.Append("<div class=\"lcnt_r\">");
                result.Append("<div class=\"meteacher\">");
                result.AppendFormat("<div class=\"metline\">特约讲师：{0}</div>", "无");
                //计算星期数
                System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("zh-CN");
                result.AppendFormat("<div class=\"metline\"  id=\"traintime\">开讲时间：{0} ({1})</div>",
                    string.Format("{0:M月dd日}", "无"), "无");

                result.Append("</div>");
                result.AppendFormat("<input id=\"hidid\" type=\"hidden\" value=\"{0}\" />", "0");
                result.AppendFormat("<input id=\"hidarea\" type=\"hidden\" value=\"{0}\" />", "");

                result.Append("<div class=\"metline\">手机号：");
                result.Append("<input name=\"txtTel\" id=\"txtTel\" type=\"text\" value=\"\" class=\"tinp booktel\" />");
                result.Append("&nbsp;<input type=\"button\" value=\"\" class=\"bookcode\" />");
                result.Append("</div>");
                result.Append("<div class=\"metline\">验证码：");
                result.Append("<input name=\"txtCode\" id=\"txtCode\" type=\"text\" class=\"tinp booknumber\" />");
                result.Append("</div>");
                result.Append("<div class=\"metsubmit\">");
                result.Append("<div class=\"metremind\">会场信息会以短信形式发送至您手机上。</div>");
                result.Append("<input id=\"submit11\" type=\"image\" src=\"/images/met_book.jpg\" />");
                result.Append("</div>");
                result.Append("</div>");
                result.Append("<div class=\"clr\"></div>");

            }

            return result.ToString();
        }

        /// <summary>
        /// 推荐视频
        /// </summary>
        /// <returns></returns>
        public string GetTJSP()
        {
            StringBuilder result = new StringBuilder();
            TrainDAL ac = new TrainDAL();
            DataSet ds = ac.GetTJSP();
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    result.AppendFormat("<li><img src=\"/images/icvrec.gif\" /><a href=\"{1}\" title=\"{2}\" target=\"_blank\">{0}</a></li>",
                        CutStr(ds.Tables[0].Rows[i]["v_title"].ToString(), 11), ds.Tables[0].Rows[i]["V_url"].ToString(), ds.Tables[0].Rows[i]["v_title"].ToString());

                }
            }
            return result.ToString();
        }

        /// <summary>
        /// 网络会场培训
        /// </summary>
        /// <returns></returns>
        public string GetWLHCLY()
        {
            StringBuilder result = new StringBuilder();
            TrainDAL ac = new TrainDAL();
            DataSet ds = ac.GetWLHCLY();
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    result.AppendFormat("<li><img src=\"/images/icspker.gif\" /><a href=\"{1}\" title=\"{2}\" target=\"_blank\">{0}</a></li>",
                        CutStr(ds.Tables[0].Rows[i]["v_title"].ToString(), 10), ds.Tables[0].Rows[i]["V_url"].ToString(), ds.Tables[0].Rows[i]["v_title"].ToString());

                }
            }
            return result.ToString();
        }

        public string CutStr(string str, int n)
        {
            string rel = str;
            try
            {
                if (str.Length > n)
                    rel = str.Substring(0, n);
            }
            catch { }
            return rel;
        }
    }
}
